﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Linq;


namespace AbatDeployer
{
    class ConfigFileReader
    {
        string xmlFile;

        public ConfigFileReader(string xmlfilePath)
        {
            xmlFile = xmlfilePath;

        }



        public AbatModel readXmlFile(string TargetEnvName)
        {
            try
            {
                AbatModel abat = new AbatModel();
                List<ABATVariable> varModel = new List<ABATVariable>();
                XDocument doc = XDocument.Load(xmlFile);

                XElement root = doc.Element("Localization");
                List<XElement> envVar = root.Elements("Environment").ToList();



                XElement TargetNode = envVar.Select(obj => obj).Where(o => o.FirstAttribute.Value.Equals(TargetEnvName)).FirstOrDefault();
                if (TargetNode == null)
                {
                    throw new Exception("Invalid environment name");
                }
                else
                {

                    abat.VariblesList = readVariablesConfig(TargetNode);
                    abat.Queuelist = readQueueConfig(TargetNode);
                    abat.UserAccountList = readServiceAccountConfig(TargetNode);
                    abat.SrvAccountList = readServiceReferenceConfig(TargetNode);
                    abat = readPath(TargetNode, abat);

                }


                return (abat);



            }

            catch (Exception e)
            {
                throw e;
            }
        }





        public AbatModel readPath(XElement TargetNode,AbatModel abat)
        {
            
            List<XElement> VarPathList=  TargetNode.Element("ABATVariables").Element("ABATPath").Elements("Path").ToList();
            List<XElement> QPathList = TargetNode.Element("ABATQueues").Element("ABATPath").Elements("Path").ToList();
            List<XElement> UAPathList = TargetNode.Element("ABATUserAccounts").Element("ABATPath").Elements("Path").ToList();
            List<XElement> SRrPathList = TargetNode.Element("ABATServiceReference").Element("ABATPath").Elements("Path").ToList();
            abat.ABATVariablesPath = VarPathList.Select(t => t.Value).ToList();
            abat.ABATQueuesPath = QPathList.Select(t => t.Value).ToList();
            abat.ABATUserAccountsPath = UAPathList.Select(t => t.Value).ToList();
            abat.ABATSRsPath = SRrPathList.Select(t => t.Value).ToList();
            return (abat);
        
        }




        public List<ABATVariable> readVariablesConfig(XElement TargetNode)
        {
            List<ABATVariable> varModel = new List<ABATVariable>();
            List<XElement> variableListNode = TargetNode.Element("ABATVariables").Element("variableList").Elements("Variable").ToList();
            foreach (XElement element in variableListNode)
            {
                ABATVariable var_O = new ABATVariable();
                var_O.Name = element.Element("Name").Value;
                var_O.desc = element.Element("Value").Value;
                var_O.value = element.Element("Description").Value;
                varModel.Add(var_O);

            }

            return (varModel);
        }

        public List<ABATQueue> readQueueConfig(XElement TargetNode)
        {
            List<ABATQueue> varModel = new List<ABATQueue>();
            List<XElement> variableListNode = TargetNode.Element("ABATQueues").Element("QueueList").Elements("Queue").ToList();
            foreach (XElement element in variableListNode)
            {
                ABATQueue var_O = new ABATQueue();
                var_O.name = element.Element("Name").Value;
                var_O.ExecutionMachine = element.Element("ExecutionMachine").Value;
              
                varModel.Add(var_O);

            }

            return (varModel);

        }

        public List<ABATUserAccount> readServiceAccountConfig(XElement TargetNode)
        {

            List<ABATUserAccount> varModel = new List<ABATUserAccount>();
            List<XElement> variableListNode = TargetNode.Element("ABATUserAccounts").Element("AccountsList").Elements("Accounts").ToList();
            foreach (XElement element in variableListNode)
            {
                ABATUserAccount var_O = new ABATUserAccount();
                var_O.name = element.Element("Name").Value;
                var_O.UserName = element.Element("UserName").Value;
                var_O.PassWord = element.Element("PassWord").Value;

                varModel.Add(var_O);

            }

            return (varModel);
        }

        public List<ABATServiceR> readServiceReferenceConfig(XElement TargetNode)
        {
            List<ABATServiceR> varModel = new List<ABATServiceR>();
            List<XElement> variableListNode = TargetNode.Element("ABATServiceReference").Element("ServiceReferenceList").Elements("ServiceReference").ToList();
            foreach (XElement element in variableListNode)
            {
                ABATServiceR var_O = new ABATServiceR();
                var_O.name = element.Element("Name").Value;
                var_O.wsdl = element.Element("WSDLUrl").Value;

                varModel.Add(var_O);

            }

            return (varModel);

        }


    }
}

